<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="2"
        :formType="type"
        labelWidth="110"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button @click="saveDialog" type="primary">确定</el-button>
    </div>
  </div>
</template>

<script>
import {
  getJPBrands,
  getJPSeries,
  getCarBuyingFactor,        //购车因素
 } from '@/api/potenCus/customer/potenCusManage';
import { searchCarBuyingFactor } from '@/api/potenCus/basedata/carBuyingFactor';
import { dictionary } from '@/utils/dictMixin';

export default {
  name: 'addJPcar',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
    };
  },
  created() {
    this.init();
    this.getBuyCarList();  //购车因素
  },
  methods: {
    init() {
    this.getJPBrandList()
      // if (this.type === 'edit') {
      //   // 更新接口需要的记录 ID
      //   this.recordId = this.rowData.testDriveId;
        // this.formList.forEach(x => {
        //   x.initialValue = this.rowData[x.fieldName];
        // });
      // }
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '竞争品牌',
          fieldName: 'failBrand',
          placeholder: '请选择',
          itemList: [],
          rules: [{ required: true, message: '请选择竞争品牌', trigger: 'change' }],
          change: val => {
            this.changeJPBrandSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '竞争车系',
          fieldName: 'failSeries',
          placeholder: '请选择',
          itemList: [],
          rules: [{ required: true, message: '请选择竞争车系', trigger: 'change' }],
        },
        {
          type: 'MULTIPLE_SELECT',
          label: '竞品关注点',
          fieldName: 'competitorList',
          placeholder: '请选择',
          // disabled: this.type === 'edit',
          itemList: [],
          rules: [{ required: true, message: '请选择竞品关注点', trigger: 'change' }],
          // request: {
          //   fetchApi: searchCarBuyingFactor,
          //   params: {},
          //   datakey: 'records',
          //   valueKey: 'basedataId',
          //   textKey: 'basedataName'
          // }
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'competitorRemark',
          placeholder: '请输入',
        },

      ];
    },
    async getBuyCarList() {
      const res = await getCarBuyingFactor();
      // console.log('购车因素', res)
      if (res.resultCode === 200) {
        this.formList.find(item =>item.fieldName === 'competitorList').itemList = res.data.map(x => ({ value: x.basedataId, text: x.basedataName }));
      }
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      console.log("保存竞品",this.formList)
      if (this.saveData !== null) {
        let params ={
          failBrand: this.formList[0].initialValue,
          failSeries: this.formList[1].initialValue,
          competitorList: this.formList[2].initialValue,
          competitorRemark: this.formList[3].initialValue,
        }

        if (this.formList.some(item => Array.isArray(item.initialValue) ? (item.initialValue.length > 0 ? true:false) : (item.initialValue?true:false))) {
          this.$emit('JPHandle', params);
          this.closeDialog('refresh');
        } else {
          this.$message.info('未选择竞品数据')
        }

      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    async getJPBrandList() {
      const res = await getJPBrands();
      console.log('竞品品牌', res)
      if (res.resultCode === 200) {
        this.formList.find(item => item.fieldName ==='failBrand').itemList = res.data.map(x => ({ value: x.failModelId, text: x.failBrand }));
      }
    },
    async changeJPBrandSelect(failModelId) {
      if (!failModelId) {
        this.formList.find(item => item.fieldName ==='failSeries').initialValue = '';
        this.formList.find(item => item.fieldName ==='failSeries').itemList = [];
      } else {
        const res = await getJPSeries(failModelId);
        console.log('竞品车系', res)
        if (res.resultCode === 200) {
          this.formList.find(item => item.fieldName ==='failSeries').itemList = res.data.map(x => ({ value: x.failModelId, text: x.failSeries }));
        }
        if (this.formList.find(item => item.fieldName ==='failSeries').initialValue ) {
          this.formList.find(item => item.fieldName ==='failSeries').initialValue = '';
        }
      }
    },
  }
};
</script>

<style lang="less" scoped>
</style>
